//
//  TabViewController2.swift
//  StarTimesON
//
//  Created by Toj on 11/15/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class TabViewController2: UIViewController {
    
    private let viewModel = TVStatementViewModel()
    private var statements: [TVStatementCellModel] = [] { didSet {
        tableView.isHidden = statements.count == 0
        guard statements.count > 0 else { return }
        tableView.reloadData()
    } }
    
    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.contentInsetAdjustmentBehavior = .never
        
        view.separatorStyle = .none
        view.backgroundColor = UIColor.white
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.rowHeight = 180.ui_mnscale
        
        view.register(TVStatementCell.cell_class, forCellReuseIdentifier: TVStatementCell.cell_identifier)
        
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let model = TVStatementCellModel(statement: TVStatementModel())
        statements = [model]
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
    }
    
    var tableViewCellAddresses: [String] = []
    
    var collectionCellAddresses: [String] = []
    
    private var _contentOffset: CGPoint = .zero
    private weak var _scrollDelegate: STTabViewScrollDelegate?
}

private let randomCount = Int(arc4random_uniform(30) + 10)
extension TabViewController2: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("-->toj: \(randomCount)")
        return randomCount // statements.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withReuseIdentifierClass: TVStatementCell.self)
        let address = cell.cm_objectAddress
        if !tableViewCellAddresses.contains(address) {
            tableViewCellAddresses.append(address)
        }
        
        cell.statement = statements[0] //statements[indexPath.row]
       
        return cell
    }
}

extension TabViewController2: UITableViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        if offset.y < 0 {
            contentOffset = .zero
            scrollDelegate?.tabView(self, scrollView: 0)
            return
        }
        let offSetY = offset.y - contentOffset.y
        contentOffset = offset
        scrollDelegate?.tabView(self, scrollView: offSetY)
    }
}

extension TabViewController2: STTabViewTargetDelegate {
    var scrollDelegate: STTabViewScrollDelegate? {
        get {
            return _scrollDelegate
        }
        set {
            _scrollDelegate = newValue
        }
    }
    
    var contentOffset: CGPoint {
        get {
            return _contentOffset
        }
        set {
            _contentOffset = newValue
        }
    }
}
